package com.fangngng.pushall.controller;

import com.fangngng.pushall.VO.IndexInitVO;
import com.fangngng.pushall.constant.ErrorCode;
import com.fangngng.pushall.dto.AddTopicRemindVO;
import com.fangngng.pushall.dto.AddTopicVO;
import com.fangngng.pushall.dto.EditTopicRemindTimeVO;
import com.fangngng.pushall.dto.EditTopicRemindVO;
import com.fangngng.pushall.dto.EditTopicVO;
import com.fangngng.pushall.dto.Result;
import com.fangngng.pushall.dto.TopicVO;
import com.fangngng.pushall.entity.Remind;
import com.fangngng.pushall.entity.RemindTime;
import com.fangngng.pushall.entity.Topic;
import com.fangngng.pushall.entity.User;
import com.fangngng.pushall.service.TopicService;
import com.fangngng.pushall.util.IpUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * topic
 *
 * @author fei.fang
 * @date 2020/7/2
 **/
@Slf4j
@RestController
public class TopicController extends BaseController {

    private static final Gson GSON = new Gson();

    @Autowired
    private TopicService topicService;



    /**
     * 订阅主题
     *
     * @param code
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping(value = "/topic/subscribe/{code}")
    public Result<String> subscribePost(@PathVariable String code) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return topicService.subscribeTopic(code, userDetail.getEmail());
    }

    /**
     * 退订
     *
     * @param code
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping(value = {"/topic/unSubscribe/{code}"})
    public Result<String> unSubscribe(@PathVariable String code) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return Result.fail("需要登陆");
        }

        return topicService.unSubscribeTopic(userDetail.getEmail(), code);
    }

    /**
     * 主题详情
     *
     * @param code
     * @return
     */
    @GetMapping(value = "/topic/detail/{code}")
    public Result<TopicVO> topicDetail(@PathVariable String code) {
        if (!StringUtils.isEmpty(code)) {
            TopicVO topic = topicService.getTopicVOByCode(code);
            return Result.ok(topic);
        }
        return Result.ok();
    }

    /**
     * 添加主题
     *
     * @param addTopicVO
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/add")
    public Result<Topic> addTopic(@RequestBody AddTopicVO addTopicVO) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (addTopicVO == null || StringUtils.isEmpty(addTopicVO.getName())) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        Topic topic = topicService.addTopic(addTopicVO, userDetail.getEmail());

        return Result.ok(topic);
    }

    /**
     * 编辑主题
     *
     * @param editTopicVO
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/edit")
    public Result<String> editTopic(@RequestBody EditTopicVO editTopicVO) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (editTopicVO == null || editTopicVO.getId() == null) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        return topicService.editTopic(editTopicVO, userDetail.getEmail());
    }

    /**
     * 删除主题
     *
     * @param topicId
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @GetMapping(value = "/topic/delete/{topicId}")
    public Result<String> deleteTopic(@PathVariable("topicId") String topicId) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (StringUtils.isEmpty(topicId)) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        Result<String> result = topicService.deleteTopic(Integer.valueOf(topicId), userDetail.getEmail());

        return result;
    }

    /**
     * 添加主题的提醒
     *
     * @param remindVO
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/remind/add")
    public Result<Remind> addTopicRemind(@RequestBody AddTopicRemindVO remindVO) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (remindVO == null || remindVO.getTopicId() == null || remindVO.getName() == null) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        Remind remind = topicService.addTopicRemind(remindVO, userDetail.getEmail());

        return Result.ok(remind);
    }

    /**
     * 编辑主题的提醒
     *
     * @param remindVO
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/remind/edit")
    public Result<Remind> editTopicRemind(@RequestBody EditTopicRemindVO remindVO) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (remindVO == null || remindVO.getTopicId() == null || remindVO.getName() == null || remindVO.getRemindId() == null) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        Remind remind = topicService.editTopicRemind(remindVO, userDetail.getEmail());

        return Result.ok(remind);
    }

    /**
     * 删除主题的提醒
     *
     * @param remindId
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/remind/delete/{remindId}")
    public Result<String> deleteTopicRemind(@PathVariable Integer remindId) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (remindId == null) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        return topicService.deleteTopicRemind(remindId, userDetail.getEmail());
    }

    /**
     * 编辑主题的提醒的时间
     *
     * @param remindVO
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/remind/time/edit")
    public Result<RemindTime> editTopicRemindTime(@RequestBody EditTopicRemindTimeVO remindVO) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (remindVO == null ) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        RemindTime remind = topicService.editTopicRemindTime(remindVO, userDetail.getEmail());

        return Result.ok(remind);
    }

    /**
     * 删除主题的提醒时间
     *
     * @param remindTimeId
     * @return
     */
    @PreAuthorize("isAuthenticated()")
    @PostMapping(value = "/topic/remind/time/delete/{remindTimeId}")
    public Result<String> deleteTopicRemindTime(@PathVariable Integer remindTimeId) {
        User userDetail = super.getCurrentUser();
        if (userDetail == null) {
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        if (remindTimeId == null) {
            return new Result<>(ErrorCode.ERROR_PARAM_ERROR);
        }

        return topicService.deleteTopicRemindTime(remindTimeId, userDetail.getEmail());
    }

}
